package com.sunday.scoremanager.ui;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;
import com.sunday.common.base.BaseActivity;
import com.sunday.common.model.ResultDO;
import com.sunday.common.utils.Constants;
import com.sunday.common.utils.ToastUtils;
import com.sunday.common.widgets.EmptyLayout;
import com.sunday.common.widgets.adapter.CommonAdapter;
import com.sunday.common.widgets.adapter.ViewHolder;
import com.sunday.common.widgets.recyclerView.HorizontalDividerItemDecoration;
import com.sunday.scoremanager.R;
import com.sunday.scoremanager.base.BaseApp;
import com.sunday.scoremanager.entity.Fans;
import com.sunday.scoremanager.entity.Message;
import com.sunday.scoremanager.http.ApiClient;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static com.sunday.common.widgets.EmptyLayout.HIDE_LAYOUT;
import static com.sunday.common.widgets.EmptyLayout.NODATA;

/**
 * Created by 刘涛 on 2017/10/27.
 */

public class MessageListActivity extends BaseActivity {


    @Bind(R.id.title_view)
    TextView titleView;
    @Bind(R.id.recycler_view)
    RecyclerView recyclerView;
    @Bind(R.id.refresh_layout)
    SmartRefreshLayout refreshLayout;
    @Bind(R.id.empty_layout)
    EmptyLayout emptyLayout;

    private List<Message> dataSet = new ArrayList<>();
    private CommonAdapter<Message> adapter;
    private LinearLayoutManager layoutManager;
    private int type;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recycler_list);
        ButterKnife.bind(this);
        initView();
        getData();

    }

    private void initView() {

        type = getIntent().getIntExtra("type", 1);
        titleView.setText(type == 1 ? "系统消息" : "收银助手");
        layoutManager = new LinearLayoutManager(mContext);
        recyclerView.setLayoutManager(layoutManager);
        adapter = new CommonAdapter<Message>(mContext, R.layout.list_message_item, dataSet) {
            @Override
            public void convert(ViewHolder holder, Message message) {
                bind(holder, message);
            }
        };
        recyclerView.setAdapter(adapter);
        recyclerView.addItemDecoration(new HorizontalDividerItemDecoration.Builder(mContext)
                .drawable(R.drawable.shape_divider)
                .build());

        emptyLayout.setErrorType(HIDE_LAYOUT);
        emptyLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getData();
            }
        });

        refreshLayout.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                if (isCanloadMore) {
                    pageNo++;
                    getData();
                } else {
                    refreshlayout.finishLoadmore();
                }
            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                pageNo = 1;
                getData();
            }
        });

    }

    private int pageNo = 1;
    private boolean isCanloadMore = false;

    private void getData() {
        Call<ResultDO<List<Message>>> call = ApiClient.getApiAdapter().getMessageList(BaseApp.getInstance().getMember().getSellerId(),
                pageNo, Constants.PAGE_SIZE, type);
        call.enqueue(new Callback<ResultDO<List<Message>>>() {
            @Override
            public void onResponse(Call<ResultDO<List<Message>>> call, Response<ResultDO<List<Message>>> response) {
                if (isFinish || response.body() == null) {
                    return;
                }
                refreshLayout.finishRefresh();
                refreshLayout.finishLoadmore();
                if (response.body().getCode() == 0) {
                    if (pageNo == 1) {
                        dataSet.clear();
                    }
                    dataSet.addAll(response.body().getResult());
                    if (response.body().getResult().size() == Constants.PAGE_SIZE) {
                        isCanloadMore = true;
                    } else {
                        isCanloadMore = false;
                    }
                    emptyLayout.setErrorType(dataSet.size() > 0 ? HIDE_LAYOUT : NODATA);
                    adapter.notifyDataSetChanged();
                }
            }

            @Override
            public void onFailure(Call<ResultDO<List<Message>>> call, Throwable t) {
                refreshLayout.finishRefresh();
                refreshLayout.finishLoadmore();
                ToastUtils.showToast(mContext, R.string.network_error);
            }
        });
    }

    private void bind(ViewHolder holder, Message message) {
        TextView msgTitle = holder.getView(R.id.content);
        TextView msgContent = holder.getView(R.id.time);
        msgTitle.setText(message.getMessage());
        msgContent.setText(message.getCreateTime());
    }
}
